package thread.bus;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;


/**
 * 模拟公交车类
 */
public class Bus implements Runnable {


    private int routes = 1;


    public Bus(String bugsName,int routes) {
        this.bugsName = bugsName;
        this.routes = routes;
    }

    private String bugsName;

    private String driverString;//司机

    private CopyOnWriteArrayList<Passenger> passengers = new CopyOnWriteArrayList<>();//乘客

    public String getDriverString() {
        return driverString;
    }

    public void setDriverString(String driverString) {
        this.driverString = driverString;
    }

    public CopyOnWriteArrayList<Passenger> getPassengers() {
        return passengers;
    }

    public void setPassengers(CopyOnWriteArrayList<Passenger> passengers) {
        this.passengers = passengers;
    }

    public String getBugsName() {
        return bugsName;
    }

    public void setBugsName(String bugsName) {
        this.bugsName = bugsName;
    }

    public int getRoutes() {
        return routes;
    }

    public void setRoutes(int routes) {
        this.routes = routes;
    }

    /**
     * 打印结果均已经实现，当前时间没有打印，公交车损坏的情况没有处理
     *
     */
    private  final static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public void run()   {

        long beginTime = System.currentTimeMillis();//当前时间
        boolean flag = true;
        while (routes >=1 && routes<=15){
            if ((System.currentTimeMillis()-beginTime)/1000/60==300){
                System.out.println(this.bugsName+",行驶了300分钟，结束");
                break;
            }
            if (flag){

                System.out.println("当前时间："+simpleDateFormat.format(new Date())+":"+this.bugsName+"，从第"+routes+"站开始上乘客");
                //从 1站 行驶到 第15站
                int  upperTotal = 0;
                for (Passenger passenger : BusMain.PASSENGERS_UPPER) {
                    if (this.passengers.size()>=constant.max_passenger_total){
                        break;
                    }
                    if (passenger.getSource() == routes) {
                        this.passengers.add(passenger);
                        upperTotal++;
                        BusMain.PASSENGERS_UPPER.remove(passenger);
                        try {
                            //上车10秒
                            TimeUnit.SECONDS.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                System.out.println("当前时间："+simpleDateFormat.format(new Date())+":"+this.bugsName+"，第"+routes+"站，乘客已经上完车，一共上车"+upperTotal+"人，开始发车");

                try {
                    //模拟每个站点行驶时间为一分钟
                    TimeUnit.SECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                routes++;
                System.out.println("当前时间："+simpleDateFormat.format(new Date())+":"+ this.bugsName+"，到达"+routes+"站");
                //每个站会有随机的人数下车
                int ltotle = 0;
                for (Passenger passenger : this.passengers) {
                   if (passenger.getTarger()==routes){
                       this.passengers.remove(passenger);
                       ltotle++;
                       try {
                           //下车10秒
                           TimeUnit.SECONDS.sleep(10);
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                   }
                }
                System.out.println("当前时间："+simpleDateFormat.format(new Date())+":"+this.bugsName+"，到达"+routes+"站，下车"+ltotle+"人");

                if (routes==15){
                    System.out.println("当前时间："+simpleDateFormat.format(new Date())+":"+this.bugsName+"，到达终点站全部乘客下车");
                    this.passengers.clear();
                    flag = false;
                }
            }else {
                //从15站行驶到第1站
                System.out.println("当前时间："+simpleDateFormat.format(new Date())+":"+this.bugsName+"从第"+routes+"站开始上乘客");
                int upTotal = 0;
                for (Passenger passenger : BusMain.PASSENGERS_LOWER) {
                    if (this.passengers.size()>=constant.max_passenger_total){
                        break;
                    }
                    if (passenger.getSource() == routes) {
                         this.passengers.add(passenger);
                         upTotal++;
                         BusMain.PASSENGERS_LOWER.remove(passenger);
                        try {
                            //上车10秒
                            TimeUnit.SECONDS.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                System.out.println("当前时间："+simpleDateFormat.format(new Date())+":"+this.bugsName+"第"+routes+"站，乘客已经上完车一共上车"+upTotal+"人，开始发车");

                try {
                    //模拟每个站点行驶时间为一分钟
                    TimeUnit.SECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                routes--;
                System.out.println("当前时间："+simpleDateFormat.format(new Date())+":"+this.bugsName+"到达"+routes+"站");
                //每个站会有随机的人数下车
                int ltotle = 0;
                for (Passenger passenger : this.passengers) {
                    if (passenger.getTarger()==routes){
                        this.passengers.remove(passenger);
                        ltotle++;
                        try {
                            //下车10秒
                            TimeUnit.SECONDS.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                System.out.println("当前时间："+simpleDateFormat.format(new Date())+":"+this.bugsName+"到达"+routes+"站,下车"+ltotle+"人");

                if (routes==1){
                    System.out.println("当前时间："+simpleDateFormat.format(new Date())+":"+this.bugsName+"到达终点站全部乘客下车");
                    this.passengers.clear();
                    flag = false;
                }
            }

        }

    }
}
